package com.crm.system.service.impl.finance.paymentRecordManager;

import com.crm.common.model.DhPageInfo;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.finance.paymentRecordManager.PaymentRecordQO;
import com.crm.model.vo.finance.paymentRecordManager.PaymentRecordExportVO;
import com.crm.model.vo.finance.paymentRecordManager.PaymentRecordVO;
import com.crm.service.finance.paymentRecordManager.PaymentRecordDbService;
import com.crm.system.service.finance.paymentRecordManager.PaymentRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/8/19 9:43
 */
@Service
public class PaymentRecordServiceImpl implements PaymentRecordService {

    @Autowired
    private PaymentRecordDbService paymentRecordDbService;

    /**
     * 财务 到款记录
     * @Author pym
     * @Date  2021/8/19 10:33
     * @param pageIndex -页码
     * @param pageSize -分页大小
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @param paymentRecordQO -查询条件
     * @return {@link {com.crm.common.model.DhPageInfo<com.crm.model.vo.finance.paymentRecordManager.PaymentRecordVO>}}
     */
    @Override
    public DhPageInfo<PaymentRecordVO> selectPaymentRecordList(Integer pageIndex, Integer pageSize, String sortName,
                                                               String sortOrder, PaymentRecordQO paymentRecordQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        List<PaymentRecordVO> paymentRecordVOList = new ArrayList<PaymentRecordVO>();
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        if (userAccount != null) {
            if ("3".equals(userAccount.getDeptId())) {//总部财务
                paymentRecordQO.setCompanyId("");
            } else {
                paymentRecordQO.setCompanyId(userAccount.getCompanyId());
            }
            //排序字符串拼接
            String order = getOrder(sortName, sortOrder);
            paymentRecordVOList = paymentRecordDbService.selectPaymentRecordList(order,paymentRecordQO);
        }
        PageInfo<PaymentRecordVO> pageInfo = new PageInfo<PaymentRecordVO>(paymentRecordVOList);
        return new DhPageInfo<PaymentRecordVO>(pageInfo,paymentRecordVOList);
    }

    /**
     * 财务 导出到款记录
     * @Author pym
     * @Date  2021/8/19 15:15
     * @param paymentRecordQO -查询条件
     * @return {@link {java.util.List<com.crm.model.vo.finance.paymentRecordManager.PaymentRecordExportVO>}}
     */
    @Override
    public List<PaymentRecordExportVO> selectPaymentRecordExportVOList(PaymentRecordQO paymentRecordQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        List<PaymentRecordExportVO> paymentRecordExportVOList = new ArrayList<PaymentRecordExportVO>();
        if (userAccount != null) {
            if ("3".equals(userAccount.getDeptId())) {//总部财务
                paymentRecordQO.setCompanyId("");
            } else {
                paymentRecordQO.setCompanyId(userAccount.getCompanyId());
            }
            paymentRecordExportVOList = paymentRecordDbService.selectPaymentRecordExportVOList(paymentRecordQO);
        }
        return paymentRecordExportVOList;
    }

    /**
     * 排序字符串拼接
     * @Author pym
     * @Date  2021/8/19 10:45
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @return {@link {java.lang.String}}
     */
    private String getOrder(String sortName, String sortOrder) {
        String order = "";
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch(sortName){
                case "orderNumber":
                { //所属订单
                    order = getOrder(order, sortOrder, "order_number");
                }
                break;
                case "customerName":
                { //所属客户
                    order = getOrder(order, sortOrder, "customer_name");
                }
                break;
                case "productName":
                { //购买产品
                    order = getOrder(order, sortOrder, "product_name");
                }
                break;
                case "arriveTime":
                { //到款日期
                    order = getOrder(order, sortOrder, "arrive_time");
                }
                break;
                case "productFinalPrice":
                { //到款金额
                    order = getOrder(order, sortOrder, "product_final_price");
                }
                break;
                case "gradeMoney":
                { //业绩金额
                    order = getOrder(order, sortOrder, "grade_money");
                }
                break;
                case "contractNumber":
                { //所属合同
                    order = getOrder(order, sortOrder, "contract_number");
                }
                break;
                case "performanceConversionRatio":
                { //业绩换算比例
                    order = getOrder(order, sortOrder, "performance_conversion_ratio");
                }
                break;
                case "contractType":
                { //合同类型
                    order = getOrder(order, sortOrder, "contract_type");
                }
                break;
                case "companyName":
                { //所属分公司
                    order = getOrder(order, sortOrder, "company_name");
                }
                break;
                case "userName":
                { //所属业务员
                    order = getOrder(order, sortOrder, "user_name");
                }
                break;
                case "royaltyStatus":
                { //提成发放状态
                    order = getOrder(order, sortOrder, "royalty_status");
                }
                break;
                case "royaltyMoney":
                { //提成金额
                    order = getOrder(order, sortOrder, "royalty_money");
                }
                break;
                case "royaltyRecordTime":
                { //提成录入时间
                    order = getOrder(order, sortOrder, "royalty_record_time");
                }
                break;
                case "productStatus":
                { //是否退款
                    order = getOrder(order, sortOrder, "product_status");
                }
                break;
                case "arriveMethod":
                { //到款方式
                    order = getOrder(order, sortOrder, "arrive_method");
                }
                break;
                case "contractStartDate":
                { //合同开始日期
                    order = getOrder(order, sortOrder, "contract_start_date");
                }
                break;
                case "arriveUnitName":
                { //到款单位
                    order = getOrder(order, sortOrder, "arrive_unit_name");
                }
                break;
                case "arriveNum":
                { //到款编号
                    order = getOrder(order, sortOrder, "arrive_num");
                }
                break;
                default:
                    break;
            }
        }
        if (!order.contains("arrive_time")) {
            order = getOrder(order, "DESC", "arrive_time");
        }
        return order;
    }
    /**
     * 排序字符串拼接
     * @Author pym
     * @Date  2021/8/19 10:45
     * @param order -排序字符串
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @return {@link {java.lang.String}}
     */
    private String getOrder(String order, String sortOrder, String sortName) {
        if (StringUtils.isBlank(order)) {
            order += sortName +" "+ sortOrder;
        } else {
            order += ","+sortName +" " + sortOrder;
        }
        return order;
    }
}
